package com.Emplyment.utils;

import com.Emplyment.entity.Article;
import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import java.util.ArrayList;
import java.util.List;

public class SearchUtil {

    public List<Article> handleResponse(SearchResponse response){
        List<Article> list = new ArrayList<>();
        // 4.解析响应
        SearchHits searchHits = response.getHits();
        // 4.1.获取总条数
        long total = searchHits.getTotalHits().value;
        System.out.println("共搜索到" + total + "条数据");
        // 4.2.文档数组
        SearchHit[] hits = searchHits.getHits();
        // 4.3.遍历
        for (SearchHit hit : hits) {
            // 获取文档source
            String json = hit.getSourceAsString();
            // 解析json
            Article article = JSONObject.parseObject(json, Article.class);
            list.add(article);
        }
        return list;
    }

}
